package com.bawei.mall.user.service.Impl;

import com.alibaba.fastjson2.JSONObject;
import com.bawei.common.core.exception.ServiceException;
import com.bawei.mall.product.domain.model.MallUserCollectInfo;
import com.bawei.mall.product.domain.request.UserCollectList;
import com.bawei.mall.user.domain.request.UserInformati;
import com.bawei.mall.user.mapper.MallUserCollectMapper;
import com.bawei.mall.user.service.MallUserCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author：张余庆
 * @Package：com.bawei.mall.user.service.Impl
 * @Project：mall_cloud
 * @name：MallUserCollectServiceImpl
 * @Date：2024/7/4 13:54
 */
@Service
public class MallUserCollectServiceImpl implements MallUserCollectService {

    @Autowired
    private MallUserCollectMapper mapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 功能：查询用户收藏表
     * @return
     */
    @Override
    public UserCollectList list(Integer pageNum, Integer pageSize) {
        String token = redisTemplate.opsForValue().get("aa");
        if (token == null || token.isEmpty()) {
            throw new ServiceException("Token为空");
        }
        String cusUserJSONString = redisTemplate.opsForValue().get(token);
        if (cusUserJSONString == null) {
            throw new ServiceException("未找到用户信息");
        }
        UserInformati userInformati = JSONObject.parseObject(cusUserJSONString, UserInformati.class);
        Long userId = userInformati.getUserId();
        UserCollectList list = new UserCollectList();
        List<MallUserCollectInfo> collect = mapper.list(pageNum,pageSize,userId);
        list.setList(collect);
        return list;
    }

    /**
     * 功能：清空用户的收藏表
     */
    @Override
    public void clear() {
        String token = redisTemplate.opsForValue().get("aa");
        if (token == null || token.isEmpty()) {
            throw new ServiceException("Token为空");
        }
        String cusUserJSONString = redisTemplate.opsForValue().get(token);
        if (cusUserJSONString == null) {
            throw new ServiceException("未找到用户信息");
        }
        UserInformati userInformati = JSONObject.parseObject(cusUserJSONString, UserInformati.class);
        Long userId = userInformati.getUserId();
        Integer i = mapper.clear(userId);
        if(i<=0){
            throw new RuntimeException("删除失败");
        }
    }

    /**
     * 功能：添加用户收藏表
     */
    @Override
    public void add(MallUserCollectInfo mallUserCollectInfo) {
        String token = redisTemplate.opsForValue().get("aa");
        if (token == null || token.isEmpty()) {
            throw new ServiceException("Token为空");
        }
        String cusUserJSONString = redisTemplate.opsForValue().get(token);
        if (cusUserJSONString == null) {
            throw new ServiceException("未找到用户信息");
        }
        UserInformati userInformati = JSONObject.parseObject(cusUserJSONString, UserInformati.class);
        Long userId = userInformati.getUserId();
        mallUserCollectInfo.setUserId(userId);
        Integer i = mapper.add(mallUserCollectInfo);
        if(i<=0){
            throw new RuntimeException("添加失败");
        }
    }
}
